/**
 * 
 */
package edu.uwm.nlp.jude.internal;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.uwm.nlp.jude.util.IRegex;
import edu.uwm.nlp.jude.util.NumberUtil;
import edu.uwm.nlp.jude.util.StringUtil;

/**
 * @author qing
 *
 * Jun 15, 2009
 */
public class ReferenceListAnalyser {
	
	private String fullRef;
								//full reference, excluding the title "Reference"
	private int ptnCode = -1;
	
	public int getPtnCode() {
		return ptnCode;
	}

	public void setPtnCode(int ptnCode) {
		this.ptnCode = ptnCode;
	}

	public ReferenceListAnalyser(String fullRef)
	{
		this.fullRef = fullRef;
	}
	
	/**
	 * split citation from the reference list
	 */
	public ArrayList<ReferenceEntity> forkFullRef()
	{
		int curIdx = 0;
		while(fullRef.indexOf(curIdx)=='\r'||fullRef.indexOf(curIdx)=='\n'||fullRef.indexOf(curIdx)==' ')
			curIdx++;
		
		
		ptnCode = this.detectStartPtn(curIdx);
		
//		System.out.println("ptnCode="+ptnCode);
		FullReferences fRef = new FullReferences(fullRef);
//		System.out.println(fullRef);
		
		ArrayList<ReferenceEntity> rEntList = null;
		
		if(ptnCode == IFullRefFormats.BRANCKET_INT)
		{
			rEntList = fRef.forkBrankedInt('[', ']');
		}
		else if(ptnCode == IFullRefFormats.PRANPH_INT)
		{
			rEntList = fRef.forkBrankedInt('(', ')');
		}
		else if(ptnCode == IFullRefFormats.INT_DOT)
		{
			rEntList = fRef.forkIntSimple('.');
		}
		else if(ptnCode == IFullRefFormats.INT_ONLY)
		{
			rEntList = fRef.forkIntSimple(' ');
		}
		else if(ptnCode == IFullRefFormats.AUTHOR)
		{
			rEntList = fRef.forkAuthor();
		}
		else if(ptnCode == IFullRefFormats.BRANCKET_AUTHOR)
		{
			rEntList = fRef.forkBrankedAuthor('[', ']');
		}
		else if(ptnCode == IFullRefFormats.INT_HALF_PRANTH)
		{
			rEntList = fRef.forkIntHalfPranph(')');
		}
		
		return rEntList;
		
	}
	
	/**
	 * detect the start pattern of the full reference
	 * @param start index
	 * @return pattern code
	 */
	public int detectStartPtn(int start)
	{
		int ptnCode = IFullRefFormats.UNCLEAR_PTN;
								// initialize ptnCode as unclear ptn. default.
		
		int curIdx = 0;
		while(fullRef.charAt(curIdx)=='\r'||fullRef.charAt(curIdx)=='\n'||fullRef.charAt(curIdx)==' ')
			curIdx++;
		char firstChar = fullRef.charAt(curIdx);
		
		if(firstChar == '[')
		{
			int nextIdx = fullRef.indexOf(']');
								// the length between [ and ] should be within a threshold
								// this threshold will be determined in the future version
			String idStr = fullRef.substring(curIdx+1,nextIdx).trim();
			if(NumberUtil.isNum(idStr))
				ptnCode = IFullRefFormats.BRANCKET_INT;
			else
				ptnCode = IFullRefFormats.BRANCKET_AUTHOR;
		}
		else if(firstChar == '(')
		{
			int nextIdx = fullRef.indexOf(')');
								// the length between [ and ] should be within a threshold
								// this threshold will be determined in the future version
			String idStr = fullRef.substring(curIdx,nextIdx).trim();
			if(NumberUtil.isNum(idStr))
				ptnCode = IFullRefFormats.PRANPH_INT;
			else
				ptnCode = IFullRefFormats.UNCLEAR_PTN;
		}
		else if(firstChar=='1')
		{
			char nextChar = StringUtil.nextNonEmptyChar(fullRef, curIdx+1);
			if(nextChar == '.')
				ptnCode = IFullRefFormats.INT_DOT;
			else if(nextChar == ')' && fullRef.lastIndexOf('(', curIdx)==-1)
				ptnCode = IFullRefFormats.INT_HALF_PRANTH;
			else
				ptnCode = IFullRefFormats.INT_ONLY;
		}
		else if(firstChar >='A'&& firstChar<='Z'|| firstChar >='a' && firstChar<='z')
		{
			Pattern p = Pattern.compile(IRegex.YEAR_REGEX);
								// not necessary in current citation, just make sure the current area has citation
			Matcher m = p.matcher(fullRef);
			if(m.find(curIdx))
				ptnCode = IFullRefFormats.AUTHOR;
			else
				ptnCode = IFullRefFormats.UNCLEAR_PTN;
		}
		
		return ptnCode;
		
	}
	
}
